package com.dayatang.hrm.compensation.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import com.dayatang.commons.domain.AbstractEntity;
import com.dayatang.commons.domain.InstanceFactory;

/**
 * 薪资项目分类
 * 
 * @author chencao
 * 
 */
@Entity
@Table(name = "pay_salary_item_type")
@NamedQueries( { @NamedQuery(name = "SalaryItemType.findBySalaryAccountSuite", query = "from SalaryItemType o where o.salaryAccountSuite = :pSuite") })
public class SalaryItemType extends AbstractEntity {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4655177230541631002L;

	private static SalaryItemTypeRepository salaryItemTypeRepository;

	// 名称
	@Column(name = "name", length = 50)
	private String name;
	// 排序号
	@Column(name = "order_seq")
	private Integer orderSeq;
	// 备注
	@Column(name = "remark", length = 200)
	private String remark;

	@ManyToOne
	@JoinColumn(name = "salary_accountsuite_id")
	private SalaryAccountSuite salaryAccountSuite;

	public SalaryItemType() {
	}

	public static SalaryItemTypeRepository getSalaryItemTypeRepository() {
		if (salaryItemTypeRepository == null) {
			salaryItemTypeRepository = InstanceFactory
					.getInstance(SalaryItemTypeRepository.class);
		}
		return salaryItemTypeRepository;
	}

	public static void setSalaryItemTypeRepository(
			SalaryItemTypeRepository salaryItemTypeRepository) {
		SalaryItemType.salaryItemTypeRepository = salaryItemTypeRepository;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getOrderSeq() {
		return orderSeq;
	}

	public void setOrderSeq(Integer orderSeq) {
		this.orderSeq = orderSeq;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public SalaryAccountSuite getSalaryAccountSuite() {
		return salaryAccountSuite;
	}

	public void setSalaryAccountSuite(SalaryAccountSuite salaryAccountSuite) {
		this.salaryAccountSuite = salaryAccountSuite;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof SalaryItemType == false) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		SalaryItemType rhs = (SalaryItemType) obj;
		return new EqualsBuilder().append(getId(), rhs.getId()).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(getId()).toHashCode();
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.SHORT_PREFIX_STYLE);
	}

	/*
	 * =======================================
	 * 
	 * action
	 * 
	 * ========================================
	 */

	public void save() {
		getSalaryItemTypeRepository().save(this);
	}

	public void remove() {
		getSalaryItemTypeRepository().remove(this);
	}

	public static SalaryItemType get(Long id) {
		return getSalaryItemTypeRepository().get(id);
	}

	/**
	 * 根据薪资帐套，查找其下的薪资项目分类
	 * 
	 * @param accountSuite
	 * @return
	 */
	public static List<SalaryItemType> findBySalaryAccountSuite(
			SalaryAccountSuite accountSuite) {
		return getSalaryItemTypeRepository().findBySalaryAccountSuite(
				accountSuite);
	}
}
